import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";
declare var $:any;
/**
 * 历史
 * zengxianxiang
 * 2017.11.27
 */
export class History{
  constructor(
    private  pointID:string,
    private  dataType:string,
    private startTime:string,
    private  endTime:string,
    private pageNum:string
  ){}
}
@Component({
  selector: 'app-history',
  templateUrl: './history.component.html',
  styleUrls: ['./history.component.css']
})
export class HistoryComponent implements OnInit {
  private tbody: Array<any>;
  private heads: Array<any>;
  private Properties: Array<any>;
  private DataList: Array<any>;
  private bussHistoricalDatas: Array<any>;
  public dimsUrl:string = this.httpURLService.getUrl("DIMS");
  public pointID: string; //点位id
  public Localhost: string;
  public pollutionInfoNum:string;
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public AirQltyHistory:History = new History("","","","","");
  public Url:string;
  public ipAddress:string;
  public time:number;
  public timer;
  public timeOne;
  public timeTwo;
  ngOnInit() {
    //调用日历控件
    this.dateRangePicker();
    this.endTimePicker();
  }
  //获取时间
  getRangeDate( range,sj) {
    const formatDate = ( time: any ) => {
      // 格式化日期，获取今天的日期
      const Dates = new Date( time );
      const year: number = Dates.getFullYear();
      const month: any = ( Dates.getMonth() + 1 ) < 10 ? '0' + ( Dates.getMonth() + 1 ) : ( Dates.getMonth() + 1 );
      const day: any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
      const Hours: any = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
      const Minutes: any = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
      const Seconds: any = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
      return year + '-' + month + '-' + day + ' ' + Hours + ':' + Minutes + ':' + Seconds;
    };
    let changeDate: string;
    changeDate = formatDate( new Date().getTime() + ( 1000 * 3600 * 24 * range ) );
    sessionStorage[sj] = changeDate;
  }
  //接受父级的值，首次加载历史
  constructor(private http: HttpClient,private httpURLService: HttpURLService) {
   this.time = 1;
    this.timer = setInterval(() => {
      this.time++;
      if(this.time == 2){
        $(".reset-btn").trigger("click");
      }
      if(this.time == 3){
        clearInterval(this.timer);
      }
    },200);

    this.getRangeDate(0,"dangqi");
    this.getRangeDate(-1,"zuotian");
    let dangqi = sessionStorage["dangqi"]; //当前时间
    let zuotian = sessionStorage["zuotian"]; //昨天时间
    this.Url = sessionStorage["url"];  //接受父级传的接口
    this.pointID = sessionStorage["pointID"]; //接收父页面传的点位id
    this.AirQltyHistory  =  new History(this.pointID,"1", zuotian,dangqi,this.curPage);
    //现场端
    if(this.Url == "/monitor/queryLocalEqupHistoricalMon"){
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.heads = data['localEquipmentInfos'].head;
        this.Properties = data['localEquipmentInfos'].properties;
        this.DataList = data['localEquipmentInfos'].dataList;
        this.pollutionInfoNum = data['pollutionInfoNum'];
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
      //工况
    }else if (this.Url == "/control/queryProcessHistoricalMon"){
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.heads = data['processInfos'].head;
        this.Properties = data['processInfos'].properties;
        this.DataList = data['processInfos'].dataList;
        this.pollutionInfoNum = data['pollutionInfoNum'];
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
    }else { //其他情况
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.heads = data['bussHistoricalDatas'].head;
        this.Properties = data['bussHistoricalDatas'].properties;
        this.DataList = data['bussHistoricalDatas'].dataList;
        this.pollutionInfoNum = data['pollutionInfoNum'];
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
    }
  }
  //按条件查询
  listMessage(info:any){
    let menuStatus = info.menuStatus== undefined ? '' : info.menuStatus;
    let statrTime = $("#startTime").val()== undefined ? '' : $("#startTime").val(); //获取并判断是否为undefined
    let endTime = $("#endTime").val()== undefined ? '' : $("#endTime").val();
    let dangqi = sessionStorage["dangqi"]; //当前时间
    let zuotian = sessionStorage["zuotian"]; //昨天时间
    this.Url = sessionStorage["url"];
    this.pointID= sessionStorage["pointID"];
    sessionStorage["menuStatus"]=menuStatus;
    sessionStorage["statrTime"]=statrTime;
    sessionStorage["endTime"]=endTime;
    if(menuStatus==""){
      menuStatus="1";
    }
    if(statrTime=="" || endTime ==""){
      statrTime=zuotian;
      endTime=dangqi;
    }
    this.AirQltyHistory  =  new History(this.pointID,menuStatus, statrTime,endTime,"1");
    //现场端
    if(this.Url == "/monitor/queryLocalEqupHistoricalMon"){
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.heads = data['localEquipmentInfos'].head;
        this.Properties = data['localEquipmentInfos'].properties;
        this.DataList = data['localEquipmentInfos'].dataList;
        this.pollutionInfoNum = data['pollutionInfoNum'];
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
      //工况
    }else if (this.Url == "/control/queryProcessHistoricalMon"){
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.heads = data['processInfos'].head;
        this.Properties = data['processInfos'].properties;
        this.DataList = data['processInfos'].dataList;
        this.pollutionInfoNum = data['pollutionInfoNum'];
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
    }else {
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.heads = data['bussHistoricalDatas'].head;
        this.Properties = data['bussHistoricalDatas'].properties;
        this.DataList = data['bussHistoricalDatas'].dataList;
        this.pollutionInfoNum = data['pollutionInfoNum'];
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
    }
  }
  //分页
  getPageData($event){
    this.curPage = $event; //当前页
    this.Url = sessionStorage["url"];
    this.pointID= sessionStorage["pointID"];
    let menuStatus = sessionStorage["menuStatus"];
    let statrTime = sessionStorage["statrTime"];
    let endTime = sessionStorage["endTime"];
    //当输入框或下拉菜单有值时
    if((menuStatus!=undefined)&&(statrTime!=undefined)&&(endTime!=undefined)){
      this.AirQltyHistory  =  new History(this.pointID,menuStatus,statrTime, endTime,this.curPage);
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        this.DataList = data['bussHistoricalDatas'].dataList;
        this.totalNum = data["page"].total;
        this.totalPage = data["page"].pages;
        this.curPage = data["page"].current;
        this.pageSize = data["page"].size;
      });
      //当输入框或下拉菜单没有值
    }else {
      this.pointID= sessionStorage["pointID"];
      this.tbody= sessionStorage["WaterHistory"];
      this.AirQltyHistory  =  new History(this.pointID,"1", "2017-12-05 14:20:58","2017-12-07 14:20:58" ,this.curPage);
      this.http.post(this.dimsUrl+""+this.Url+"",this.AirQltyHistory).subscribe(data => {
        //现场端
        if(this.Url == "/monitor/queryLocalEqupHistoricalMon"){
          this.heads = data['localEquipmentInfos'].head;
          this.Properties = data['localEquipmentInfos'].properties;
          this.DataList = data['localEquipmentInfos'].dataList;
          this.totalNum = data["page"].total;
          this.totalPage = data["page"].pages;
          this.curPage = data["page"].current;
          this.pageSize = data["page"].size;
          //工况
        }else if (this.Url == "/control/queryProcessHistoricalMon"){
          this.heads = data['processInfos'].head;
          this.Properties = data['processInfos'].properties;
          this.DataList = data['processInfos'].dataList;
          this.totalNum = data["page"].total;
          this.totalPage = data["page"].pages;
          this.curPage = data["page"].current;
          this.pageSize = data["page"].size;
        }else {//其他
          this.DataList = data['bussHistoricalDatas'].dataList;
          this.totalNum = data["page"].total;
          this.totalPage = data["page"].pages;
          this.curPage = data["page"].current;
          this.pageSize = data["page"].size;
        }
      });
    }
  }
  //日历控件
  endTimePicker(){
    let picker:any=$('#endTime');
    let dataRageOption:Object={
      "singleDatePicker":true,
      "timePicker": true,
      "timePicker24Hour": true,
      "locale": {
        "format": "YYYY-MM-DD HH:mm:ss",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "customRangeLabel": "Custom",
        "daysOfWeek": ["日","一","二","三","四","五","六"],
        "monthNames": ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
      }
    };
    picker.daterangepicker(dataRageOption,function (start,end,label) {
      this.startTime = start.format('YYYY-MM-DD HH:mm:ss');
      console.log(`start:${start.format('YYYY-MM-DD HH:mm:ss')}, end:${end}, label:${label}`);
    });
  }
  dateRangePicker(){
    let picker:any=$('#startTime');
    let dataRageOption:Object={
      "singleDatePicker":true,
      "timePicker": true,
      "timePicker24Hour": true,
      "locale": {
        "format": "YYYY-MM-DD HH:mm:ss",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "customRangeLabel": "Custom",
        "daysOfWeek": ["日","一","二","三","四","五","六"],
        "monthNames": ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
      }
    };
    picker.daterangepicker(dataRageOption,function (start,end,label) {
      console.log(`start:${start.format('YYYY-MM-DD HH:mm:ss')}, end:${end}, label:${label}`);
    });
  }
  //返回父级页面
  Back(){
    window.history.back();
  }
  //判断是否为数组
  isArray(arr){
    return Array.isArray(arr);
  }
  //导出表
  export() {

    let url = sessionStorage["url"]; //获取父页面的接口
    let pointID = sessionStorage["pointID"];//点位id
    let dangqi = sessionStorage["dangqi"];   //获取当天时间
    let zuotian = sessionStorage["zuotian"];//获取昨天时间
    let menuStatus = sessionStorage["menuStatus"]; //接收重点、非重点
    let statrTime = sessionStorage["statrTime"];//接收开始时间
    let endTime = sessionStorage["endTime"];//接受、收结束时间
    
    let Newdangqi = dangqi.replace(" ","%"); //当前时间
    let Newzuotian = zuotian.replace(" ","%");  //昨天时间

    let NewstatrTime = statrTime.replace(" ","%"); //开始时间
    let NewendTime = endTime.replace(" ","%");  //结束时间
    //判断当输入框或下拉菜单有值
    if(menuStatus==""){
      menuStatus="1";
    }
    if(statrTime=="" || endTime ==""){
      this.timeOne = Newzuotian;
      this.timeTwo = Newdangqi;
    }else {
      this.timeOne = NewstatrTime;
      this.timeTwo = NewendTime;
    }


      if(url == "/units/queryWaterHistoricalMon"){ //水
        window.open(this.dimsUrl+"/units/exportWaterHistory?pointID="+pointID+"&startTime="+this.timeOne+"&endTime="+this.timeTwo+"&dataType="+menuStatus+"");
      }
      if(url == "/units/queryAirHistoricalMon"){ //气
        window.open(this.dimsUrl+"/units/exportAirHistory?pointID="+pointID+"&startTime="+this.timeOne+"&endTime="+this.timeTwo+"&dataType="+menuStatus+"");
      }
      if(url == "/units/queryVOCHistoricalMon"){ //VOC
        window.open(this.dimsUrl+"/units/exportVocAirHistory?pointID="+pointID+"&startTime="+this.timeOne+"&endTime="+this.timeTwo+"&dataType="+menuStatus+"");
      }
      if(url == "/units/querySewageHistoricalMon"){ //污水
        window.open(this.dimsUrl+"/units/exportPolluterWaterHistory?pointID="+pointID+"&startTime="+this.timeOne+"&endTime="+this.timeTwo+"&dataType="+menuStatus+"");
      }
      if(url == "/environment/queryAirQltyHistoricalMon"){ //空气质量
        window.open(this.dimsUrl+"/environment/exportAirHistory?pointID="+pointID+"&startTime="+this.timeOne+"&endTime="+this.timeTwo+"&dataType="+menuStatus+"");
      }
      if(url == "/environment/querySurfWaterHistoricalMon"){ //地表水
        window.open(this.dimsUrl+"/environment/exportWaterHistory?pointID="+pointID+"&startTime="+this.timeOne+"&endTime="+this.timeTwo+"&dataType="+menuStatus+"");
      }



  }
}
